package com.dev.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.dev.security.entity.Org;
import com.dev.security.entity.Role;
import com.dev.security.mapper.OrgMapper;
import com.dev.security.service.OrgService;
import com.dev.security.vo.OrgVo;
import com.dev.security.vo.PageVo;
import com.dev.security.vo.TreeVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.reflect.generics.tree.Tree;

import java.util.Date;
import java.util.List;

/**
 * 机构Service实现
 *
 * @author yuboon
 * @version v1.0
 * @date 2020/04/04
 */
@Service
public class OrgServiceImpl implements OrgService {

    @Autowired
    private OrgMapper orgMapper;

    @Override
    @Transactional
    public int save(OrgVo orgVo) {
        Org orgEntity = new Org();
        // hutool
        BeanUtil.copyProperties(orgVo,orgEntity);
        orgEntity.setCreateDate(new Date());
        return orgMapper.insert(orgEntity);
    }

    @Override
    @Transactional
    public int update(OrgVo roleVo) {
        Org orgEntity = new Org();
        BeanUtil.copyProperties(roleVo,orgEntity);
        return orgMapper.update(orgEntity);
    }

    @Override
    @Transactional
    public int remove(int id) {
        return orgMapper.delete(id);
    }

    @Override
    public int batchRemove(String ids) {
        JSONArray jsonArray = JSONUtil.parseArray(ids);
        for(Object id : jsonArray){
            orgMapper.delete(Convert.toInt(id));
        }
        return jsonArray.size();
    }

    @Override
    public PageVo<OrgVo> selectList(OrgVo orgVo, PageVo pageVo) {
        if(StrUtil.isNotEmpty(orgVo.getCode())){
            orgVo.setCode(orgVo.getCode() + "%");
        }

        List<OrgVo> listVo = CollectionUtil.newArrayList();
        // 分页
        Page page = PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());

        Org org = new Org();
        BeanUtil.copyProperties(orgVo,org);
        List<Org> listEntity = orgMapper.selectList(org);
        for(Org entity : listEntity){
            OrgVo vo = new OrgVo();
            BeanUtil.copyProperties(entity,vo);
            vo.setCreateDate(DateUtil.formatDate(entity.getCreateDate()));
            listVo.add(vo);
        }

        pageVo.setList(listVo);
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }

    @Override
    public List<TreeVo> selectTree(){
        List<Org> orgList = orgMapper.selectList(new Org());
        List<TreeVo> treeVoList = CollectionUtil.newArrayList();

        for(Org org : orgList){
            if(org.getParentId() == 0){
                TreeVo treeVo = new TreeVo();
                treeVo.setId(org.getId());
                treeVo.setTitle(org.getName());
                treeVoList.add(treeVo);
                buildTree(treeVo,orgList);
            }
        }
        return treeVoList;
    }

    private void buildTree(TreeVo parentVo,List<Org> orgList){
        for(Org org : orgList){
            if(org.getParentId() == parentVo.getId()){
                TreeVo childVo = new TreeVo();
                childVo.setId(org.getId());
                childVo.setTitle(org.getName());

                List<TreeVo> childList = parentVo.getChildren();
                if(childList == null){
                    childList = CollectionUtil.newArrayList();
                    parentVo.setChildren(childList);
                }

                childList.add(childVo);
                buildTree(childVo,orgList);
            }
        }
    }


}
